package com.glodon.paas.document.web.resource;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;

import com.glodon.paas.consts.StringConst;
import com.glodon.paas.document.api.bean.File;
import com.glodon.paas.document.service.RecyclerService;
import com.glodon.paas.exception.PaasServerExceptionFactory;
import com.glodon.paas.exception.i18n.ErrorCode;
import com.glodon.paas.web.resource.BaseResource;

public abstract class RecyclerResource implements BaseResource {

    @Autowired
    private RecyclerService recyclerService;

    @GET
    @Path("/")
    @Produces(MediaType.APPLICATION_JSON)
    public List<File> getFilesInRecycler(@Context
    HttpServletRequest request) {
        return recyclerService.getFiles(getUserId(request));
    }

    @GET
    @Path("/{fileName}")
    @Produces(MediaType.APPLICATION_JSON)
    public Object get(@Context
    HttpServletRequest request, @PathParam("fileName")
    String fileName, @QueryParam("type")
    String type) {
        if ("id".equalsIgnoreCase(type)) {
            return recyclerService.getFile(getUserId(request), fileName);
        } else {
            return recyclerService.getFiles(getUserId(request), fileName);
        }
    }

    // @POST
    // @Path("/")
    // @Produces(MediaType.APPLICATION_JSON)
    // public String restoreRecycler(@Context HttpServletRequest request) {
    // List<File> files = recyclerService.getFiles(getUserId(request));
    // if(files != null){
    // for(File file : files){
    // recyclerService.restore(getUserId(request), file.getId());
    // }
    // }
    // return StringConst.SUCCESS;
    // }

    @POST
    @Path("/{fileName}")
    @Produces(MediaType.TEXT_PLAIN)
    public String restore(@Context
    HttpServletRequest request, @PathParam("fileName")
    String fileName, @QueryParam("type")
    String type) {
        String userId = getUserId(request);
        if ("id".equalsIgnoreCase(type)) {
            recyclerService.restore(userId, fileName);
        } else {
            List<File> files = recyclerService.getFiles(userId, fileName);
            if (files == null || files.size() == 0) {
                throw PaasServerExceptionFactory.notFoundException("deleted file[" + fileName + "] not exist.", null).setErrorCode(ErrorCode.FILE_NOT_EXIST);
            }
            if (files.size() > 1) {
                throw PaasServerExceptionFactory.externalAPIException("too many file[name:" + fileName
                                                                      + "] in recycler, can not restore them.", null);
            } else {
                recyclerService.restore(userId, files.get(0).getId());
            }
        }

        return StringConst.SUCCESS;
    }

    @DELETE
    @Path("/")
    @Produces(MediaType.APPLICATION_JSON)
    public String cleanRecycler(@Context
    HttpServletRequest request) {
        recyclerService.clean(getUserId(request));
        return StringConst.SUCCESS;
    }

    @DELETE
    @Path("/{fileName}")
    @Produces(MediaType.TEXT_PLAIN)
    public String delete(@Context
    HttpServletRequest request, @PathParam("fileName")
    String fileName, @QueryParam("type")
    String type, @QueryParam("force")
    String force) {
        String userId = getUserId(request);
        if ("id".equalsIgnoreCase(type)) {
            recyclerService.remove(userId, fileName);
        } else {
            List<File> files = recyclerService.getFiles(userId, fileName);
            if (files.size() > 1) {
                if (Boolean.valueOf(force)) {
                    for (File file : files) {
                        recyclerService.remove(userId, file.getId());
                    }
                } else {
                    throw PaasServerExceptionFactory.externalAPIException("too many file[name:"
                                                                                  + fileName
                                                                                  + "] in recycler, please force delete them.",
                                                                          null);
                }
            } else {
                recyclerService.remove(userId, files.get(0).getId());
            }
        }
        return StringConst.SUCCESS;
    }

}
